import getUserTempId from "@/utils/userTempId";
import { reqLogin, reqUserInfo, reqLogout } from "@/api/index";
const state = {
    userTempId: getUserTempId(),
    token: localStorage.getItem("token") || "",
    userInfo: {
        nickName: "",
    },
};
const mutations = {
    SET_TOKEN_STATE(state, payload) {
        state.token = payload.token;
    },
    SET_USETINFO_STATE(state, payload) {
        state.userInfo = payload;
    },
    RESET_USERINFO_STATE(state) {
        state.token = "";
        state.userInfo = {
            nickName: "",
        };
        localStorage.removeItem("token");
    },
};
const actions = {
    async getToken({ commit }, userInfo) {
        try {
            const result = await reqLogin(userInfo);
            commit("SET_TOKEN_STATE", result);
            localStorage.setItem("token", result.token);
        } catch (error) {
            alert(error.message);
            return Promise.reject();
        }
    },
    async getUserInfo({ commit }) {
        try {
            const result = await reqUserInfo();
            commit("SET_USETINFO_STATE", result);
        } catch (e) {
            return Promise.reject(e);
        }
    },
    getResetUser({ commit }) {
        commit("RESET_USERINFO_STATE");
    },
    async getLogout({ commit }) {
        try {
            await reqLogout();
            //退出登录成功以后,还是要清空所有的用户信息
            commit("RESET_USERINFO_STATE");
        } catch (e) {
            return Promise.reject(e);
        }
    },
};
export default {
    namespaced: true,
    state,
    mutations,
    actions,
};
